package cliente.GUI;

import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.*;
import cliente.validar;
import util.SocketManager;

public class VentanaUsuario extends JFrame
{

	/**
	 * @param args
	 */
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PanelUsuario pU;
	private PanelContrasenya pC;
	private PanelIP pI;
	String uu;
	String ip;
	boolean usuario;
	boolean pass;
	private SocketManager sm;
	
	public VentanaUsuario(boolean a, boolean c, String u, String IP, SocketManager s)
	{
		pU = new PanelUsuario();
		pC = new PanelContrasenya();
		pI = new PanelIP();
		uu = u;
		ip = IP;
		
		usuario = a;
		pass = c;
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	    this.setResizable(false);
	    this.setAlwaysOnTop(false);
	    this.setLocationRelativeTo(null);
	    
	    if(!usuario && !pass)
	    	this.add(pI);
	    else
	    	if(usuario && !pass)
	    		this.add(pU);
	    	else
	    		if(usuario && pass)
	    			this.add(pC);
	    this.pack();
	    
	    pU.Aceptar.addActionListener(new escuchador());
	    pC.Aceptar.addActionListener(new escuchador());
	    pI.Aceptar.addActionListener(new escuchador());
	    
	    pU.Cancelar.addActionListener(new escuchador());
	    pC.Cancelar.addActionListener(new escuchador());
	    pI.Cancelar.addActionListener(new escuchador());
	    
	    pU.usuario.addKeyListener(new KeyAdapter()
	    {
	    	public void keyPressed(KeyEvent e)
	    	{
	    		int key = e.getKeyCode();
	    		if (key == KeyEvent.VK_ENTER)
	    		{
	    			pU.Aceptar.doClick();
	    			}
	    		}
	    	}
	    );
	    
	    pC.contrasenya.addKeyListener(new KeyAdapter()
	    {
	    	public void keyPressed(KeyEvent e)
	    	{
	    		int key = e.getKeyCode();
	    		if (key == KeyEvent.VK_ENTER)
	    		{
	    			pC.Aceptar.doClick();
	    		}
	    	}
         }
      );
	    
	    pI.ip.addKeyListener(new KeyAdapter()
	    {
	    	public void keyPressed(KeyEvent e)
	    	{
	    		int key = e.getKeyCode();
	    		if (key == KeyEvent.VK_ENTER)
	    		{
	    			pI.Aceptar.doClick();
	    			}
	    		}
	    	}
	    );
	}
	
	public static void main(String []args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
	{
		UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
		VentanaUsuario v = new VentanaUsuario(false, false, "", "", null);
		v.setVisible(true);
	}
	
	class escuchador implements ActionListener
	{
		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent e)
		{
			String u = uu;
			String p = "";
			
			if(e.getActionCommand() == "Aceptar")
			{
				if(usuario && !pass)
				{
					try
					{
						u = "USER "+ pU.usuario.getText();
						
						String s = validar.validarusuario(u);
						
						if(s.charAt(0) != '4')
						{
							pass = !pass;
							VentanaUsuario v = new VentanaUsuario(usuario, pass, u, ip, sm);
							v.setVisible(true);
							dispose();
						}
						else
							pU.usuario.setText("");
					}
					catch (SQLException e1)
					{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1)
					{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				else
				{
					if(pass)
					{
						try
						{
							p = "PASS "+ pC.contrasenya.getText();
							String s = validar.validarusuarioycontrasenya(u, p);
							if(s.charAt(0) != '4')
							{
								SocketManager sm = new SocketManager(ip, 6968);
					            VentanaTren v = new VentanaTren(sm);
					            v.setVisible(true);
					            dispose();
							}
							else
								pC.contrasenya.setText("");
						}
						catch (SQLException e1)
						{
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (ClassNotFoundException e1)
						{
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					else
					{
						try
				        {
							usuario = !usuario;
							VentanaUsuario v = new VentanaUsuario(usuario, pass, u, ip, sm);
							v.setVisible(true);
							dispose();
				        }
				        catch (Exception e1)
				        {
							System.err.println("main: " + e1);
							e1.printStackTrace();
				        }
					}
				}
			}
			else
			{
				dispose();
			}
		}
	}

}
